// pages/search/product/order/order.js
// const cityselect = require('../../../../utils/cityselect.js')
const citys = require('../../../../utils/city.js')
// console.log(cityselect)
// console.log(citys)
const app = getApp();
var Constant = require('../../../../utils/constant.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
      indexP: 0,
      indexN: 0,
      indexS: 0,
      indexDate:0,
      weChatImg:'http://delice.riki.cn/mavista/img/wechat-pay-icon.png',
      pickS:[],
      pickN:[],
      pickP:[],
      phoneNum:'',
      address:'',
      consignee:'',
      coupon:'',
      status:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log(options.index);
      let price = parseInt(options.price).toFixed(2);
      
      let index = options.index;
      // let price = 0;
      // if (index==1){
      //     price = 148 
      // }else if(index==2){
      //   price = 268
      // }else if(index==3){
      //   price=378
      // }else if(index==4){
      //   price = 478
      // }else {
      //   price = 558
      // }
      let _this = this;
      let pickP = [];
      let citylist = citys.city.citylist
      for(let i = 0;i <citylist.length;i++){
        pickP[i] = citylist[i].p
      }
      let pickN = [];
      for (let i = 0; i < citylist[0].c.length;i++){
        pickN[i] = citylist[0].c[i].n
      }
      let pickS = [];
      if (citylist[0].c[0].a){
        for (let i = 0; i < citylist[0].c[0].a.length; i++) {
          pickS[i] = citylist[0].c[0].a[i].s
        }
      }
      

      // console.log(pickP);
      // console.log(pickN)
      // console.log(pickS)
      console.log(price)
      console.log(typeof price)
      _this.setData({
        citylist:citylist,
        pickS: pickS,
        pickN: pickN,
        pickP: pickP,
        price:price,
        index:index
      })
      let date = {};
    
      app.requestData('/api/purchase/deliveryDate/settings',date,'GET',function(res){
        // console.log(res)
        _this.setData({
          dates:res.dates
        })
        let date2 = {
          'city': _this.data.citylist[0].c[0].n,
          'deliveryDate': res.dates[0].value
        }
        app.requestData('/api/purchase/getReceiveDate', date2, 'GET', function (res) {
          console.log(res)
          _this.setData({
            receiveDate: res.receiveDate
          })
        },
          function (res) {
            console.log(res)
          })
      },
      function(res){
        console.log(res)
      })
      
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  bindRegionChange(e){
    this.setData({
      region:e.detail.value
    })
  },
  // pickP(e){
  //   console.log('P',e)
  //   let _this = this;
  //   let pickN = [];
  //   let pickS = [];
  //   let citylist = this.data.citylist
  //   let indexP = e.detail.value
  //   for (let i= 0; i < citylist[indexP].c.length;i++){
  //     pickN[i] = citylist[indexP].c[i].n
  //   }
  //   for (let i = 0; i < citylist[indexP].c.length; i++) {
  //     pickN[i] = citylist[indexP].c[i].n
  //   }
  //   if (citylist[indexP].c[0].a){
  //     for (let i = 0; i < citylist[indexP].c[0].a.length; i++) {
  //       pickS[i] = citylist[indexP].c[0].a[i].s
  //     }

  //     this.setData({
  //       indexP: indexP,
  //       pickN: pickN,
  //       pickS:pickS,
  //       pickSBoolean: true,
  //       indexN:0,
  //       indexS:0,
  //       indexDate:0
  //     })

  //     let addressDate = {
  //       'city': _this.data.pickN[_this.data.indexN],
  //       'deliveryDate': _this.data.dates[_this.data.indexDate].value
  //     }

  //     app.requestData('/api/purchase/getReceiveDate', addressDate, 'GET', function (res) {
  //       console.log('receiveDate1111', res);
  //       _this.setData({
  //         receiveDate: res.receiveDate
  //       })
  //     }, function (res) {

  //     })
  //   }else {
  //     this.setData({
  //       indexP: indexP,
  //       pickN: pickN,
  //       pickSBoolean: false,
  //       indexN:0,
  //       indexS:0,
  //       indexDate:0
  //     })
  //   }
  // },
  // pickN(e){
  //   console.log('N',e)
  //   let _this = this;
  //   let indexN = e.detail.value;
  //   let indexP = this.data.indexP;
  //   let pickS = []
  //   let citylist = this.data.citylist;
  //   if (citylist[indexP].c[indexN].a){
  //     for (let i = 0; i < citylist[indexP].c[indexN].a.length; i++) {
  //       pickS[i] = citylist[indexP].c[indexN].a[i].s
  //     }
      
  //     _this.setData({
  //       indexN: indexN,
  //       indexS:0,
  //       pickS: pickS,
  //       pickSBoolean:true
  //     })

  //     let addressDate = {
  //       'city': _this.data.pickN[_this.data.indexN],
  //       'deliveryDate': _this.data.dates[_this.data.indexDate].value
  //     }
  //     console.log(addressDate)
  //     app.requestData('/api/purchase/getReceiveDate', addressDate, 'GET', function (res) {
  //       console.log('receiveDate', res);
  //       _this.setData({
  //         receiveDate: res.receiveDate
  //       })
  //     }, function (res) {

  //     })
      
  //   }else {
  //     this.setData({
  //       indexN: indexN
  //     })
  //   }
    
 
  // },
  // pickS(e) {
  //   console.log('S', e)
    
  //   let indexS = e.detail.value;
  //   this.setData({
  //     indexS: e.detail.value
  //   })
  // },
  pickAddress(e){
    let _this = this;
    let citylist = _this.data.citylist;
    console.log(e)
    if (e.currentTarget.id=='pickP'){
      let pickN = [];
      let pickS = [];
      let indexP = e.detail.value;
      for (let i = 0; i < citylist[indexP].c.length; i++) {
        pickN[i] = citylist[indexP].c[i].n
      }
      if (citylist[indexP].c[0].a){
          for (let i = 0; i < citylist[indexP].c[0].a.length; i++) {
            pickS[i] = citylist[indexP].c[0].a[i].s
          }
          let addressDate = {
            'city': _this.data.pickN[_this.data.indexN],
            'deliveryDate': _this.data.dates[_this.data.indexDate].value
          }
          app.requestData('/api/purchase/getReceiveDate', addressDate, 'GET', function (res) {
            console.log('receiveDate1111', res);
            _this.setData({
              receiveDate: res.receiveDate
            })
          }, function (res) {

          })
      this.setData({
        indexP: indexP,
        pickN: pickN,
        pickS:pickS,
        pickSBoolean: true,
        indexN:0,
        indexS:0,
        indexDate:0
      })
    }else {
      this.setData({
        indexP: indexP,
        pickN: pickN,
        pickSBoolean: false,
        indexN:0,
        indexDate:0
      })
    }
    }else if(e.currentTarget.id=='pickN'){
      let indexN = e.detail.value;
      let indexP = this.data.indexP;
      let pickS = [];
      if (citylist[indexP].c[indexN].a) {
        for (let i = 0; i < citylist[indexP].c[indexN].a.length; i++) {
          pickS[i] = citylist[indexP].c[indexN].a[i].s
        }
        let addressDate = {
          'city': _this.data.pickN[_this.data.indexN],
          'deliveryDate': _this.data.dates[_this.data.indexDate].value
        }
        console.log(addressDate)
        app.requestData('/api/purchase/getReceiveDate', addressDate, 'GET', function (res) {
          console.log('receiveDate', res);
          _this.setData({
            receiveDate: res.receiveDate
          })
        }, function (res) {

        })
        _this.setData({
          indexN: indexN,
          indexS: 0,
          pickS: pickS,
          pickSBoolean: true
        })
      } else {
        this.setData({
          indexN: indexN
        })
      }
    } else if (e.currentTarget.id == 'pickS') {

      let indexS = e.detail.value;
      this.setData({
        indexS: e.detail.value
      })
    }
  },
  showDate(e){
      this.setData({
        show:true
      })
  },
  pickDate(e){
    console.log(e)
    let _this = this;
    let addressDate = {
      'city': _this.data.pickN[_this.data.indexN],
      'deliveryDate': _this.data.dates[e.currentTarget.dataset.index].value
    }
    app.requestData('/api/purchase/getReceiveDate', addressDate, 'GET', function (res) {
      console.log('receiveDate2222222222', res);
      _this.setData({
        receiveDate: res.receiveDate
      })
    }, function (res) {

    })
    this.setData({
      indexDate: e.currentTarget.dataset.index,
      show:false
    })
  },
  closeDate(){
    this.setData({
      show:false
    })
  },
  couponInput(e){
    this.setData({
      coupon:e.detail.value
    })
  },
coupon(e){
  let coupon = { coupon: this.data.coupon};
  let _this  = this;
  app.requestData('/api/purchase/coupon/validate',coupon,'GET',function(res){
    console.log(res)
    console.log(res)
    if(res.status=='error'){
      wx.showToast({
        image:'../../../../images/gantanhao.png',
        title: '验证码无效',
      })
    _this.setData({
      coupon:'',

    })
    }else if(res.status=='ok'){
      wx.showToast({
        title: '验证码正确',
      })
      let couponPrice = (_this.data.price *  res.discount / 100).toFixed(2);
      let price2 = (_this.data.price * (1-res.discount / 100)).toFixed(2)
      _this.setData({
        couponInfo:res,
        status:true,
        couponPrice: couponPrice,
        price2:price2
      })
    }
  },function(res){

  })
},
  orderAdd(e){
    let _this = this;
    let authCode = wx.getStorageSync('authCode');
    let url = '/api/purchase/order/add/BOX_OPTION_'+_this.data.index;
    let district= _this.data.pickS[_this.data.indexS] ||'';
   
    if (_this.data.consignee){
      let reg = /^1[0-9]{10}$/;
      let phoneNum = _this.data.phoneNum;
      let flag = reg.test(phoneNum);
      console.log(flag)
      if (flag){
        

        if (_this.data.address){
          let orderCon = {
            name: _this.data.consignee,
            phone:_this.data.phoneNum,
            province:_this.data.pickP[_this.data.indexP],
            city: _this.data.pickN[_this.data.indexN],
            district: district,
            deliveryDate: _this.data.dates[_this.data.indexDate].value,
            coupon:_this.data.coupon,
            authCode:authCode,
            address:_this.data.address
          }
          console.log(orderCon);
          app.requestData(url, orderCon, 'POST', function (res) {
            
            
              console.log('order added testing',res);
              if (res.status == 'ok') {
                wx.navigateTo({
                  url: '../orderDetail/orderDetail',
                })
              }
            
          },function(fail){
            console.log(fail)
          });
        }else {
          wx.showToast({
            image:'../../../../images/gantanhao.png',
            title: '请填写具体地址',
          })
          return
        }
      }else {
        wx.showToast({
          image: '../../../../images/gantanhao.png',
          title: '请填写正确电话',
        })
        return
      }
    }else {
      wx.showToast({
        image: '../../../../images/gantanhao.png',
        title: '请填写收货人',
      })
     
      return
    }
    
  },
  bindPhone(e){
    this.setData({
      phoneNum: e.detail.value
    })
    
  },
  bindName(e) {
    this.setData({
      consignee: e.detail.value
    })
  },
   bindAddress(e) {
    this.setData({
      address: e.detail.value
    })
  },
   anotherCoupon(){
     this.setData({
       status:false,
     })
   }
})